﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Formula_Detail
    Inherits System.Web.UI.UserControl

    Private strPlayerUrl As String
    Private strFileUrl As String
    Private strImageUrl As String

#Region "Property"
    Public Property PlayerUrl() As String
        Get
            Return strPlayerUrl
        End Get
        Set(ByVal Value As String)
            strPlayerUrl = Value
        End Set
    End Property
    Public Property FileUrl() As String
        Get
            Return strFileUrl
        End Get
        Set(ByVal Value As String)
            strFileUrl = Value
        End Set
    End Property
    Public Property ImageUrl() As String
        Get
            Return strImageUrl
        End Get
        Set(ByVal Value As String)
            strImageUrl = Value
        End Set
    End Property
#End Region
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id") Is Nothing Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim p As Formula = (From q In data.Formulae Where q.Id = hidID.Value Select q).SingleOrDefault
        If Not p Is Nothing Then
            lblTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
            lblDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
            lblMaterial.Text = IIf(IsNothing(p.Material) = True, "", p.Material)
            'Lay nhung nguyen lieu trong bang quan he tuong ung voi id nay
            Dim strMaterial As String = ""
            Dim pMaterial = (From q In data.FormulaMaterials Where q.FormulaId = hidID.Value Select q).ToList
            If pMaterial.Count > 0 Then
                For i As Integer = 0 To pMaterial.Count - 1
                    strMaterial = strMaterial & pMaterial(i).Material.Title & "; "
                Next
            End If
            lblMaterialSelect.Text = strMaterial.Trim()
            lblWaytocook.Text = IIf(IsNothing(p.WayToCook) = True, "", p.WayToCook)
            'Lay nhung cach thuc hien trong bang quan he tuong ung voi id nay
            Dim strWayToCook As String = ""
            Dim pWayToCook = (From q In data.FormulaWayToCooks Where q.FormulaId = hidID.Value Select q).ToList
            If pWayToCook.Count > 0 Then
                For i As Integer = 0 To pWayToCook.Count - 1
                    strWayToCook = strWayToCook & pWayToCook(i).WayToCook.Title & "; "
                Next
            End If
            lblWaytocookSelect.Text = strWayToCook.Trim()
            'lblUrlclip.Text = IIf(IsNothing(p.URLClip) = True, "", p.URLClip)
            chkYoutube.Checked = IIf(IsNothing(p.IsYoutube) = True, False, p.IsYoutube)
            lblFormulatypeid.Text = IIf(IsNothing(p.FormulaType.Title) = True, "", p.FormulaType.Title)
            lblFastconduct.Text = IIf(IsNothing(p.FastConduct) = True, "", p.FastConduct)
            lblPortion.Text = IIf(IsNothing(p.Portion) = True, "", p.Portion & " Người")
            lblTimepreparing.Text = IIf(IsNothing(p.TimePreparing) = True, "", p.TimePreparing & " Phút")
            lblTimetocook.Text = IIf(IsNothing(p.TimeToCook) = True, "", p.TimeToCook & " Phút")
            lblCost.Text = IIf(IsNothing(p.Cost) = True, "", p.Cost & " Đồng")
            lblStatusid.Text = IIf(IsNothing(p.Status.Title) = True, "", p.Status.Title)
            lblCreator.Text = IIf(IsNothing(p.Creator) = True, "", p.Creator)
            lblCreated.Text = IIf(IsNothing(p.Created) = True, "", Convert.ToDateTime(p.Created).ToString("dd/MM/yyyy"))
            lblModified.Text = IIf(IsNothing(p.Modified) = True, "", Convert.ToDateTime(p.Modified).ToString("dd/MM/yyyy"))
            lblModifier.Text = IIf(IsNothing(p.Modifier) = True, "", p.Modifier)
            lblPublisheddate.Text = IIf(IsNothing(p.PublishedDate) = True, "", Convert.ToDateTime(p.PublishedDate).ToString("dd/MM/yyyy"))
            'lblBeffectivedate.Text = IIf(IsNothing(p.bEffectiveDate) = True, "", Convert.ToDateTime(p.bEffectiveDate).ToString("dd/MM/yyyy"))
            lblEeffectivedate.Text = IIf(IsNothing(p.eEffectiveDate) = True, "", Convert.ToDateTime(p.eEffectiveDate).ToString("dd/MM/yyyy"))
            lblDateontv.Text = IIf(IsNothing(p.DateOnTV) = True, "", Convert.ToDateTime(p.DateOnTV).ToString("dd/MM/yyyy"))
            lblVoted.Text = IIf(IsNothing(p.Voted) = True, "", p.Voted)
            lblViewed.Text = IIf(IsNothing(p.Viewed) = True, "", p.Viewed)

            'Hien thi clip
            If p.IsYoutube = True Then
                Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                divEmbededVideo.Visible = False
                divFileYoutube.Visible = True
                ltrShow.Text = p.URL
                'FileUrl() = SplitVideoYoutube(p.URL)
                'ImageUrl() = baseUrl + ResolveUrl_Video(p.Image)
            Else
                divEmbededVideo.Visible = True
                divFileYoutube.Visible = False
                Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                PlayerUrl() = baseUrl + ResolveUrl_Video("~/jwplayer/player.swf")
                FileUrl() = p.URL
                ImageUrl() = p.Image
            End If
            chkIsHot.Checked = IIf(IsNothing(p.IsHot) = True, False, p.IsHot)
            chkIsNew.Checked = IIf(IsNothing(p.IsNew) = True, False, p.IsNew)
            imageBig.ImageUrl = IIf(IsNothing(p.ImageBig) = True, "~/images/NoPhotoAvailable.jpg", p.ImageBig)
            img.ImageUrl = IIf(IsNothing(p.Image) = True, "~/images/NoPhotoAvailable.jpg", p.Image)

        End If
    End Sub
    Protected Function SplitVideoYoutube(ByVal strUrl As String) As String
        Dim intStart As Integer = strUrl.IndexOf("=")
        Dim intEnd As Integer = strUrl.IndexOf("&")
        Dim scr As String = strUrl.Substring(intStart + 1, strUrl.Length - intStart - 1)
        Return scr
    End Function
    Public Function ResolveUrl_Video(ByVal url As String) As String
        If url Is Nothing Then
            Return ""
        End If
        If url.Length = 0 Then
            Return ""
        End If

        ' there is no ~ in the first character position, just return the url 
        If url(0) <> "~"c Then
            Return url
        End If

        Dim applicationPath As String = HttpContext.Current.Request.ApplicationPath

        ' there is just the ~ in the URL, return the applicatonPath 
        If url.Length = 1 Then
            Return applicationPath
        End If

        ' assume url looks like ~somePage 
        Dim indexOfUrl As Integer = 1

        ' determine the middle character 
        Dim midPath As String = If((applicationPath.Length > 1), "/", String.Empty)

        ' if url looks like ~/ or ~\ change the indexOfUrl to 2 
        If url(1) = "/"c OrElse url(1) = "\"c Then
            indexOfUrl = 2
        End If

        Return applicationPath & midPath & url.Substring(indexOfUrl)
    End Function
#End Region
End Class
